package com.example.mcp.server;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import com.microsoft.playwright.Response;
import com.microsoft.playwright.options.AriaRole;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Example {

    private static Logger logger = LoggerFactory.getLogger(Example.class);

    public static void main(String[] args) {

        try (Playwright playwright = Playwright.create()) {
            Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions()
                    .setHeadless(false));
            BrowserContext context = browser.newContext();
            Page page = context.newPage();
            page.navigate("https://www.xiaohongshu.com/explore");

            // 读取终端输入的信息
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("请扫描二维码:");
            try {
                String input = reader.readLine();
                System.out.println("您输入的内容是: " + input);
            } catch (IOException e) {
                System.err.println("读取输入时出错: " + e.getMessage());
            }

            Page page1 = page.waitForPopup(() -> {
                page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions().setName("发布")).click();
            });
            Response navigate = page1.navigate("https://creator.xiaohongshu.com/publish/publish?source=official");
            TimeUnit.SECONDS.sleep(5);
            page1.getByText("上传图文").nth(1).click();
            logger.info("navigate: {}", navigate);
            TimeUnit.SECONDS.sleep(5);
//            page1.getByRole(AriaRole.TEXTBOX).click();
            logger.info("click TEXTBOX");
            TimeUnit.SECONDS.sleep(5);

            page1.screenshot(new Page.ScreenshotOptions().setPath(Paths.get("screenshot-webkit.png")));
            // 原来的写法会匹配到两个 BUTTON，改成精确匹配 Choose File 的 input
            page1.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Choose File"))
                    .setInputFiles(Paths.get("screenshot-webkit.png"));
            logger.info("setInputFiles");
            TimeUnit.SECONDS.sleep(5);
            page1.getByPlaceholder("填写标题会有更多赞哦～").click();
            logger.info("click placeholder");
            TimeUnit.SECONDS.sleep(5);
            page1.getByPlaceholder("填写标题会有更多赞哦～").fill("12222");
            logger.info("填写标题会有更多赞哦");
            TimeUnit.SECONDS.sleep(5);
            page1.getByRole(AriaRole.TEXTBOX).nth(1).click();
            logger.info("click TEXTBOX");
            TimeUnit.SECONDS.sleep(5);
            page1.getByRole(AriaRole.TEXTBOX).nth(1).fill("11112222");
            logger.info("11112222");
            TimeUnit.SECONDS.sleep(5);
            page1.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("发布")).click();
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

    }
}